const express = require('express');
const db = require('../utils/db')

const myRouter = express.Router();

myRouter.post('/add', (req, res) => {
    console.log(req.body)
    let c=req.body.zzdx
    if(c=='小桌'){
c=1
    }
    if(c=='中桌'){
      c=2
          }
          if(c=='大桌'){
            c=3
                }
    
    let sql=`INSERT INTO booking_order(book_name,book_personnum,book_telephone,book_person_sex,book_note,book_status,book_table_size,book_eate_time,book_wait_time)
    VALUES('${req.body.name}',${req.body.numb},'${req.body.dianhua}','${req.body.sex}','${req.body.beizhu}',${req.body.yuyue},${c},'${req.body.riqi}','${req.body.time}');`
    db(sql, function(err, data) {
      if (err) {
          res.send({ code: 500, msg: '增加失败'})
      } else {
          res.send({ code: 200, msg: '增加失败'})
      }
    })
})

myRouter.post('/xougai', (req, res) => {
  console.log(req.body.data);
  console.log(req.body.yuyue)
  // res.send({ code: 200, msg: '成功' })

  
  let mysql = `UPDATE booking_order SET book_status=${req.body.data.yuyue} WHERE book_name='${req.body.data.name}'`
  db(mysql,null,function (err, data) {
      if (err) {
        console.log(err);
          return res.json({
              code: 500,
              msg: '数据库操作失败,请稍后重试'
          })
      }
      res.json({
          code: 200,
          msg: '修改库存成功',
          data: data
      })

  })
})

myRouter.get('/cha', (req, res) => {
  let mysql = `SELECT  * FROM booking_order JOIN table_area ON book_table_size=table_id;`
  
  db(mysql, function(err, data) {
    if (err) {
        res.send({ code: 500, msg: '查询失败' })
    } else {
        res.send({ code: 200, data })
    }
})
})

myRouter.get('/charen', (req, res) => {
  console.log(req.query)
  res.send({ code: 200, msg: '成功' })
})


module.exports = myRouter;